//
//  LoadingView.m
//  SMP
//
//  Created by Shappy Say on 5/6/13.
//  Copyright (c) 2013 HSBC. All rights reserved.
//

#import "LoadingView.h"
//#import "AppDelegate.h"
#import <QuartzCore/QuartzCore.h>

@implementation LoadingView

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    [super dealloc];
}

- (id)init {
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        
        vAlpha = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
        vAlpha.backgroundColor = [UIColor blackColor];
        vAlpha.alpha = .3;
        vAlpha.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
        vAlpha.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [self addSubview:vAlpha];
        
        vContainer = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
        vContainer.backgroundColor = [UIColor blackColor];
        vContainer.alpha = .8;
        vContainer.layer.masksToBounds = YES;
        vContainer.layer.cornerRadius = 8.0;
        
        activity=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        [vContainer addSubview:activity];
        activity.center=vContainer.center;
        activity.hidesWhenStopped=YES;
        
        lblInfomation = [[UILabel alloc] initWithFrame:CGRectMake(6, 115, 149,20)];
        [vContainer addSubview:lblInfomation];
        lblInfomation.backgroundColor = [UIColor clearColor];
        lblInfomation.textColor = [UIColor whiteColor];
        lblInfomation.textAlignment = NSTextAlignmentCenter;
        lblInfomation.hidden = YES;
        lblInfomation.numberOfLines = 0;
        lblInfomation.font = [UIFont systemFontOfSize:12];
        
        progress = [[UIProgressView alloc] initWithFrame:CGRectMake(5, 144, 150, 9)];
        progress.progress = 0;
        progress.hidden = YES;
        [vContainer addSubview:progress];
        
        [self addSubview:vContainer];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(adjustFrameWithOrientation:) name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
    }
    return self;
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */

- (void)showLoading:(NSString *)s {
    NSLog(@"************** show loading with text %@ *******************",s);
    [self initOrientation];
    UIApplication *app = [UIApplication sharedApplication];
    self.frame = [[app delegate] window].bounds;
    
    if(!s || [s isEqualToString:@""]){
        vContainer.frame = CGRectMake(0, 0, 70, 70);
        lblInfomation.hidden = YES;
    }else{
        lblInfomation.hidden = NO;
        lblInfomation.text = s;
        vContainer.frame = CGRectMake(0, 0, 160, 160);
    }
    progress.hidden = YES;
    
    activity.center = vContainer.center;
    vContainer.center = self.center;
    [[[app delegate] window] addSubview:self];
    [activity startAnimating];
}

- (void)hideLoading {
    //    [UIView animateWithDuration:0.3
    //                     animations:^{
    //                         self.alpha = 0;
    //                     }
    //                     completion:^(BOOL finished){
    //                         [self removeFromSuperview];
    //                         self.alpha = 1;
    //                     }];
    [self removeFromSuperview];
    self.alpha = 1;
    
}

- (void)showProgress:(NSString *)s percentage:(float)percentage {
    [self initOrientation];
    UIApplication *app = [UIApplication sharedApplication];
    self.frame = [[app delegate] window].bounds;
    
    if(!s || [s isEqualToString:@""]){
        lblInfomation.hidden = YES;
    }else{
        lblInfomation.hidden = NO;
        lblInfomation.text = s;
    }
    vContainer.frame = CGRectMake(0, 0, 160, 160);
    progress.hidden = NO;
    progress.progress = percentage;
    activity.center = vContainer.center;
    vContainer.center = self.center;
    [[[app delegate] window] addSubview:self];
    [activity startAnimating];
}

#pragma mark - rotation process
- (void)initOrientation {
    _interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
    CGAffineTransform transform_ = CGAffineTransformMakeRotation(0) ;
    if (_interfaceOrientation == UIInterfaceOrientationPortrait) {
        transform_ = CGAffineTransformMakeRotation(0);
        _rotationValue = 0;
    }
    if (_interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        transform_ = CGAffineTransformMakeRotation(M_PI);
        _rotationValue = M_PI;
    }
    if (_interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
        transform_ = CGAffineTransformMakeRotation(-M_PI/2);
        _rotationValue = -M_PI/2;
    }
    if (_interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        transform_ = CGAffineTransformMakeRotation(M_PI/2);
        _rotationValue = M_PI/2;
    }
    vContainer.transform = transform_;
    firstTansform = transform_;
}

- (void)adjustFrameWithOrientation:(NSNotification *)notification {
    UIInterfaceOrientation toInterfaceOrientation = [[[notification userInfo] objectForKey:@"UIApplicationStatusBarOrientationUserInfoKey"] intValue];
    
    CGFloat newRotationValue = [self getRotationWithNewInterfaceOrientation:toInterfaceOrientation]+ _rotationValue;
    _rotationValue = newRotationValue;
    CGAffineTransform transform_ = CGAffineTransformMakeRotation(_rotationValue);
    //CGAffineTransform transform_ = CGAffineTransformMakeRotation(M_PI/2);
    vContainer.transform = transform_;
    _interfaceOrientation = toInterfaceOrientation;
    
    //	[imgViewForBK setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    //	[imgViewForFront setFrame:CGRectMake((imgViewForBK.frame.size.width - imgViewForFront.frame.size.width)/2, (imgViewForBK.frame.size.height - imgViewForFront.frame.size.height)/2, imgViewForFront.frame.size.width, imgViewForFront.frame.size.height)];
}

- (CGFloat)getRotationWithNewInterfaceOrientation:(UIInterfaceOrientation)newInterfaceOrientation {
    if (_interfaceOrientation == UIInterfaceOrientationPortrait) {
        if (newInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
            return -M_PI/2;
        }else if (newInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
            return M_PI/2;
        }else{
            return M_PI;
        }
    }
    
    if (_interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        if (newInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
            return M_PI/2;
        }else
            if (newInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
                return -M_PI/2;
            }else{
                return M_PI;
            }
    }
    
    if (_interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
        if (newInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
            return -M_PI/2;
        }else
            if (newInterfaceOrientation == UIInterfaceOrientationPortrait) {
                return M_PI/2;
            }else{
                return M_PI;
            }
    }
    if (_interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        if (newInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
            return M_PI/2;
        }else
            if (newInterfaceOrientation == UIInterfaceOrientationPortrait) {
                return -M_PI/2;
            }else{
                return M_PI;
            }
    }
    return 0;
}

@end
